#include "GlobalConfig.h"
#include "Util.h"

wxFileConfig* GlobalConfig::fileConfig = NULL;

void GlobalConfig::Initialize(const wxString& filePath)
{
    fileConfig = new wxFileConfig(wxEmptyString, wxEmptyString, filePath, wxEmptyString, wxCONFIG_USE_LOCAL_FILE|wxCONFIG_USE_RELATIVE_PATH);
}

void GlobalConfig::Destroy()
{
    wxDELETE(fileConfig);
}

int GlobalConfig::GetLanguage()
{
	return fileConfig->Read(_T("language"), -1);
}

void GlobalConfig::SetLanguage(int lang)
{
	fileConfig->Write(_T("language"), lang);
    fileConfig->Flush();
}


wxString GlobalConfig::GetRecentUser()
{
    return fileConfig->Read(_T("user"));
}

void GlobalConfig::SetRecentUser(const wxString& passport)
{
    fileConfig->Write(_T("user"), passport);
    fileConfig->Flush();
}

wxString GlobalConfig::GetRecentServer()
{
    return fileConfig->Read(_T("server"));
}

void GlobalConfig::SetRecentServer(const wxString& server)
{
    fileConfig->Write(_T("server"), server);
    fileConfig->Flush();
}

int GlobalConfig::GetConnectionType()
{
	return fileConfig->Read(_T("connection"), -1);

}

void GlobalConfig::SetConnectionType(int type)
{
	fileConfig->Write(_T("connection"), type);
    fileConfig->Flush();
}


wxString GlobalConfig::GetHttpProxyAddress()
{
	return fileConfig->Read(_T("httpProxyAddress"));
}

void GlobalConfig::SetHttpProxyAddress(const wxString& address)
{
	fileConfig->Write(_T("httpProxyAddress"), address);
    fileConfig->Flush();
}

int GlobalConfig::GetHttpProxyPort()
{
	return fileConfig->Read(_T("httpProxyPort"), -1);
}

void GlobalConfig::SetHttpProxyPort(int port)
{
	fileConfig->Write(_T("httpProxyPort"), port);
    fileConfig->Flush();
}

wxString GlobalConfig::GetHttpProxyUsername()
{
	return fileConfig->Read(_T("httpProxyUsername"));
}

void GlobalConfig::SetHttpProxyUsername(const wxString& username)
{
	fileConfig->Write(_T("httpProxyUsername"), username);
    fileConfig->Flush();
}

wxString GlobalConfig::GetHttpProxyPassword()
{
	wxString encryptPassword = fileConfig->Read(_T("httpProxyPassword"));
	wxString password;
	StringUtil::Base64_Decode(encryptPassword, password);
	return password;
}

void GlobalConfig::SetHttpProxyPassword(const wxString& password)
{
	wxString encryptPassword;
	StringUtil::Base64_Encode(password, encryptPassword);

	fileConfig->Write(_T("httpProxyPassword"), encryptPassword);
    fileConfig->Flush();
}
